package com.horoscope.astrology.zodiac.signs.free.activity

import android.content.Intent
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.view.View
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.NumerologyBean
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener
import com.horoscope.astrology.zodiac.signs.free.utils.ShareManager
import com.horoscope.astrology.zodiac.signs.free.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_number.*
import kotlinx.android.synthetic.main.item_loading.*
import kotlinx.android.synthetic.main.item_share.*

/**
 * 各数字具体释义
 */
class NumberActivity : BaseActivity() {

    var title: String = ""
    var num: String = ""
    var type = 0
    var isFirstLogin = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_number)

        showLoading()

        initView()

        // 只有Daily Number必展示，其它三个数字补位
        if (title == getString(R.string.text_nun1)) {
            activityShowAd("Interstitial_Today_Number")
        } else if (AppSettingManager.readInt("supplement", 0) == 1) {
            activityShowAd("Supplement")
        }

    }

    private fun initView() {
        initTitleBar(this, R.id.btn_back_edit_name, R.id.text_view_back_title, "")

        if (intent != null) {
            num = intent.getStringExtra("num") ?: "0"
            title = intent.getStringExtra("title") ?: "Number"
            isFirstLogin = intent.getBooleanExtra("isFirstLogin", false)

            when (title) {
                getString(R.string.text_nun1) -> type = 1
                getString(R.string.text_nun2) -> {
                    type = 2
                    img_num.background = getDrawable(R.mipmap.pink_alpha_circle)
                }
                getString(R.string.text_nun3) -> {
                    type = 3
                    img_num.background = getDrawable(R.mipmap.orange_alpha_circle)
                }
                getString(R.string.text_nun4) -> {
                    type = 4
                    img_num.background = getDrawable(R.mipmap.blue_alpha_circle)
                }
            }

            tv_num.text = num
            text_view_number_title.text = title.replace("\n", " ")

            // TextView滑动
            tv_text.movementMethod = ScrollingMovementMethod.getInstance()
        }

        ShareManager.initShare(
            this@NumberActivity,
            null,
            null,
            null,
            image_view_more_share,
            tv_facebook
        )

        if (isFirstLogin && AppSettingManager.readString(title, "").isNotEmpty()) {
            showLoaded()
            tv_text.text = AppSettingManager.readString(title, "")
        } else {
            getNumberText()
        }

    }

    private fun getNumberText() {
        NetworkEngine.getNumberText(type, num.toInt(), object : NetworkListener<NumerologyBean>() {
            override fun onSuccess(t: NumerologyBean) {
                if (t.data.text.isNotEmpty()) {
                    tv_text.text = t.data.text
                    AppSettingManager.saveString(t.data.text, title)
                }
                showLoaded()
            }

            override fun onError(e: String?) {
                super.onError(e)
                showLoaded()
                AppSettingManager.saveString(null, title)
                ToastUtil.showText(
                    this@NumberActivity,
                    getString(R.string.tips_reward_video_failed_to_load)
                )
            }

            override fun onFailed(t: NumerologyBean) {
                super.onFailed(t)
                showLoaded()
                AppSettingManager.saveString(null, title)
                ToastUtil.showText(
                    this@NumberActivity,
                    getString(R.string.tips_reward_video_failed_to_load)
                )
            }
        })
    }

    override fun showLoaded() {
        loading?.clearAnimation()
        loading_item.visibility = View.INVISIBLE
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        showLoading()
        initView()
        getNumberText()
        // 只有Daily Number必展示，其它三个数字补位
        if (title == "Daily Number") {
            activityShowAd("Interstitial_Today_Number")
        } else if (AppSettingManager.readInt("supplement", 0) == 1) {
            activityShowAd("Supplement")
        }
    }

}
